//
//  UserBaseViewController.m
//  YoucApp
//
//  Created by Liam on 11/24/12.
//
//

#import "UserBaseViewController.h"
#import "MBProgressHUD.h"
#import "Util.h"
#import "UserManager.h"
#import "YoucApiError.h"
#import "UIAlertView+Modal.h"
#import "iOSToast.h"
#import "UIUtil.h"

@interface UserBaseViewController ()

@property (nonatomic, assign) MBProgressHUD *hud;

- (void)showWaitingLogin;

@end

@implementation UserBaseViewController

@synthesize hud;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
	
	self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"wy_user_bg.png"]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLoginDidFinished:) name:UMUserLoginDidFinishedNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
	UIView * inputView = [self findInputFirstResponder:self.view];
	
	if (inputView)
	{
		[inputView resignFirstResponder];
	}
	
	[super viewWillDisappear:animated];
	
	[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)dissmiss
{
	[[UserManager instance] savePassword:[self getPassword]];
	
	[self dismissModalViewControllerAnimated:YES];
}

- (NSString *)getPassword
{
	return nil;
}

- (void)loginSucess:(NSNumber *)numberSucess
{
	BOOL bSucess = [numberSucess boolValue];
	
	[MBProgressHUD hideHUDForView:[Util mainWindow] animated:YES];
	
	if (bSucess)
	{
		[self performSelector:@selector(dissmiss) withObject:nil afterDelay:0.1];
	}
}

- (void)userLoginDidFinished:(NSNotification *)notification
{
	if (![NSThread isMainThread])
	{
		[self performSelectorOnMainThread:@selector(userLoginDidFinished:) withObject:notification waitUntilDone:NO];
		return;
	}
	
	YARespUserLogin *userLogin = notification.object;
	
	NSString *labelText = nil;
	NSString *detailsLabelText = nil;
	BOOL bRet = NO;
	
	switch ([userLogin.error code]) {
		case YoucApiErrorSuccess:
			labelText = [NSString stringWithFormat:@"%@成功", self.title];
			bRet = YES;
			break;
		default:
			labelText = [NSString stringWithFormat:@"%@失败", self.title];
			detailsLabelText = [userLogin.error localizedDescription];
			break;
	}
	
	self.hud.labelText = labelText;
	self.hud.detailsLabelText = detailsLabelText;
	
	self.hud.customView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
	self.hud.mode = MBProgressHUDModeCustomView;
	
	[self performSelector:@selector(loginSucess:) withObject:[NSNumber numberWithBool:bRet] afterDelay:1.5];
}

- (void)showWaitingLogin
{
	self.hud = [MBProgressHUD showHUDAddedTo:[Util mainWindow] animated:YES];
	self.hud.labelText = [NSString stringWithFormat:@"%@中...", self.title];
}

- (IBAction)inputDone
{
	UIView * inputView = [self findInputFirstResponder:self.view];
	
	if (inputView)
	{
		[inputView resignFirstResponder];
	}
	
	[self showWaitingLogin];
}

- (void)showAlert:(NSString *)message
{
	UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"登录"
													 message:message
													delegate:nil
										   cancelButtonTitle:@"确定"
										   otherButtonTitles:nil] autorelease];
	
	[alert showModal];
}

- (BOOL)isValidTextField:(UITextField *)textField checkSelector:(SEL)checkSelector errMsg:(NSString *)msg
{
	NSString *text = textField.text;
	
	NSNumber *boolNumber = [self performSelector:checkSelector withObject:text];
	
	BOOL bRet = [boolNumber boolValue];
	
	if (!bRet)
	{
		//[self showAlert:msg];
		[self.view makeToast:msg duration:1.5f position:kDefaultPosition];
		[textField becomeFirstResponder];
	}
	
	return bRet;
}

@end
